<?php
add_action('wp_head', 'user_login');
function user_login(){?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        function isEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };
       var frm = $('#login_form');
       frm.submit(function(event){
           event.preventDefault();
           var user_name = $('input[name="txt_username"]').val();
           var pwd = $('input[name="txt_pwd"]').val();
           var redirect_to = $('input[name="redirect_to"]').val();
           var user_cookie = $('input[name="user-cookie"]').val();
           var value_sending = [user_name,pwd,redirect_to,user_cookie];
           var data ={
                    action : 'login',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    if(result == 'fail'){
                        $('p.login_fail').removeClass('hiden');
                    }else{
                       window.location =result; 
                    }
                });
       });
       var frm = $('#signup_form');
       frm.submit(function(event){
           event.preventDefault();
           var user_name = $('input[name="txt_username_signup"]').val();
           var email = $('input[name="txt_email"]').val();
           var pwd = $('input[name="txt_pwd_up"]').val();
           var cf_pwd = $('input[name="txt_cf_pwd"]').val();
           var redirect_to = $('input[name="redirect_to"]').val();
           var value_sending = [user_name,email,pwd,redirect_to];
           var data ={
                    action : 'signup',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    if(result == 'signup_fail'){
                        return false;
                    }else{
                        window.location = result;
                    }
                });
       });
       
       $('input[name="txt_username_signup"]').keyup(function(){
           var user_name = $('input[name="txt_username_signup"]').val();
           if(user_name.length > 3){
                var value_sending = user_name;
                var data = {
                    action : 'check_username',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    var input = jQuery('input[name="txt_username_signup"]');
                    var exists = jQuery('span.is_exists');
                    var ready = jQuery('span.is_ready');
                    if(result == 'notexists'){
                        if(input.val().length == 0){
                            input.addClass('error_input');
                        }else{
                            input.removeClass('error_input').addClass('good_input');
                            exists.addClass('hiden');
                            ready.removeClass('hiden');
                        }
                    }else{
                        input.removeClass('good_input').addClass('error_input');
                        ready.addClass('hiden');
                        exists.removeClass('hiden');
                    }
                });
           }
        });
       $('input[name="txt_email"]').blur(function(){
           var email = $('input[name="txt_email"]').val();
                var value_sending = email;
                var data = {
                    action : 'check_email',
                    value_send : value_sending
                };
                jQuery.post(ajaxurl, data, function(result) {
                    var input = jQuery('input[name="txt_email"]');
                    var exists = jQuery('span.email_exists');
                    var ready = jQuery('span.email_ready');
                    if(result == 'notexists'){
                        if(input.val().length == 0){
                            input.addClass('error_input');
                        }else{
                            input.removeClass('error_input').addClass('good_input');
                            exists.addClass('hiden');
                            ready.removeClass('hiden');
                        }
                    }else{
                        input.removeClass('good_input').addClass('error_input');
                        ready.addClass('hiden');
                        exists.removeClass('hiden');
                    }
                    if(!isEmail(input.val()) && input.val().length >0){
                        input.removeClass('error_input').removeClass('good_input');
                        exists.addClass('hiden');
                        ready.addClass('hiden');
                        $('span.email_not_valid').removeClass('hiden');
                    }else{
                        $('span.email_not_valid').addClass('hiden');
                    }
                });
        });
       $('button[name="btn_signup"]').click(function() {
           var user = $('input[name="txt_username_signup"]');
           var email = $('input[name="txt_email"]');
           var pwd = $('input[name="txt_pwd_up"]');
           var cf_pwd = $('input[name="txt_cf_pwd"]');
           if(pwd.val() != cf_pwd.val()){
               $('p.cf_pwd_false').removeClass('hiden');
               return false;
           }
           if(user.val().length == 0){
               user.addClass('error_input');
           }else{
               user.removeClass('error_input');
           }
           if(email.val().length == 0){
               email.addClass('error_input');
           }else{
               email.removeClass('error_input');
           }
           if(pwd.val().length == 0){
               pwd.addClass('error_input');
           }else{
               pwd.removeClass('error_input');
           }
           if(cf_pwd.val().length == 0){
               cf_pwd.addClass('error_input');
           }else{
               cf_pwd.removeClass('error_input');
           }
           
            if(user.val().length != 0 && email.val().length != 0 && pwd.val().length != 0 && cf_pwd.val().length != 0){
                return true;
            }
            return false;
       });
    });
</script>
<?php }
add_action('wp_ajax_nopriv_signup', 'signup_callback');
add_action('wp_ajax_signup', 'signup_callback');
function signup_callback(){
    $value_received = $_POST['value_send'];
    $user_name = $value_received[0];
    $email = $value_received[1];
    $pwd = $value_received[2];
    $redirect_to = $value_received[3];
    $user_id = username_exists( $user_name );
     if( !$user_id and email_exists($email) == false ) {
            $user_id = wp_create_user( $user_name,'',$email );
            wp_set_password( $pwd, $user_id );
            wp_set_auth_cookie($user_id);
            echo $redirect_to;
    }else{
        echo 'signup_fail';
    }
    die();
}
add_action('wp_ajax_nopriv_check_username', 'check_username_callback');
add_action('wp_ajax_check_username', 'check_username_callback');
function check_username_callback(){
   $value_received = $_POST['value_send'];
   if(!username_exists( $value_received)){
       echo 'notexists';
   }else{
       echo 'existed';
   }
   die();
}
add_action('wp_ajax_nopriv_check_email', 'check_email_callback');
add_action('wp_ajax_check_email', 'check_email_callback');
function check_email_callback(){
   $value_received = $_POST['value_send'];
   if(email_exists($value_received) == false){
       echo 'notexists';
   }else{
       echo 'existed';
   }
   die();
}
add_action('wp_ajax_nopriv_login', 'login_callback');
add_action('wp_ajax_login', 'login_callback');
function login_callback(){
    $value_received = $_POST['value_send'];
    $user_name = $value_received[0];
    $pwd = ($value_received[1]);
    $redirect_to = $value_received[2];

    $user = get_user_by( 'login', $user_name );
    if (!user_pass_ok( $user_name, $pwd )) {
        echo "fail";
      }else{
          wp_set_auth_cookie($user->ID);
          echo $redirect_to;
      }
    die();
}
?>